package demo.controller;

import demo.pojo.Book;
import demo.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

import java.util.UUID;

@Controller
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookController {
    private final BookService bookService;
    @RequestMapping("/")
    public String Home()
    {
        return "forward:/all";
    }

    @RequestMapping("/book")
    public ModelAndView findBookById(Integer id)
    {
        Book book  = bookService.findBookById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("book");
        modelAndView.addObject("book",book);
        return modelAndView;
    }
    @RequestMapping("/all")
    public ModelAndView findAllBook()
    {
        List<Book> books = bookService.findAllBook();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("allBook");
        modelAndView.addObject("books",books);
        return modelAndView;
    }
    @RequestMapping("/update")
    public ModelAndView updateBook(HttpServletRequest request)
    {
        Integer id = Integer.valueOf(request.getParameter("id"));
        System.out.println(id);
        Book book = bookService.findBookById(id);
        System.out.println(book);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("book",book);
        modelAndView.setViewName("bookUpdate");
        return modelAndView;
    }
    @RequestMapping("/delete")
    public String delete(Integer id)
    {
        bookService.deleteById(id);
        return "forward:/book/all";
    }
@RequestMapping("/updateImage")
public String updateItemById(Book book, @RequestParam("filename") MultipartFile pictureFile) throws Exception {
    // 处理上传逻辑...
    // 图片上传
    System.out.println("11111");
    if(!pictureFile.isEmpty())
    {
        // 设置图片名称，不能重复，可以使用uuid
        String picName = UUID.randomUUID().toString();
        // 获取文件名
        String oriName = pictureFile.getOriginalFilename();
        // 获取图片后缀
        String extName = oriName.substring(oriName.lastIndexOf("."));

        // 开始上传
        pictureFile.transferTo(new File("E:\\java\\ssm-B\\src\\main\\webapp\\images\\" + picName + extName));

        // 设置图片名到商品中
        book.setImage(picName + extName);
    }
    else book.setImage(bookService.findBookById(book.getId()).getImage());

    // ---------------------------------------------
    // 更新商品
    this.bookService.updateBook(book);

    return "forward:/book/all";
}
    @RequestMapping("/searchLike")
    public ModelAndView searchLike(String name)
    {
        List<Book> books = bookService.searchLike(name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("allBook");
        modelAndView.addObject("books",books);
        return modelAndView;
    }

    @RequestMapping("/PreInsertBook")
    public String preInsertBook()
    {
        return "insertBook";
    }

    @RequestMapping("/insertBook")
    public String inertBook(Book book, @RequestParam("filename") MultipartFile pictureFile) throws IOException {
        System.out.println("11111");
        if(!pictureFile.isEmpty())
        {
            // 设置图片名称，不能重复，可以使用uuid
            String picName = UUID.randomUUID().toString();
            // 获取文件名
            String oriName = pictureFile.getOriginalFilename();
            // 获取图片后缀
            String extName = oriName.substring(oriName.lastIndexOf("."));

            // 开始上传
            pictureFile.transferTo(new File("E:\\java\\ssm-B\\src\\main\\webapp\\images\\" + picName + extName));

            // 设置图片名到商品中
            book.setImage(picName + extName);
        }
        bookService.insertBook(book);
        return "forward:/book/all";
    }

}
